package com.ruoyi.oar.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oar.mapper.OarSmsMapper;
import com.ruoyi.oar.domain.OarSms;
import com.ruoyi.oar.domain.dto.OarSmsDTO;
import com.ruoyi.oar.domain.query.OarSmsQuery;
import com.ruoyi.oar.service.OarSmsService;
import java.util.stream.Collectors;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.Optional;

/**
 * 短信日志Service业务层处理
 * 
 * @author oar
 * @date 2022-02-24
 */
@Service
public class OarSmsServiceImpl implements OarSmsService
{
    @Autowired
    private OarSmsMapper oarSmsMapper;

    /**
     * 查询短信日志
     * 
     * @param id 短信日志主键
     * @return 短信日志
     */
    @Override
    public Optional<OarSmsDTO> selectOarSmsById(String id) {
        return oarSmsMapper.selectOarSmsById(id).map(oarSms -> toDto(oarSms,true));
    }

    /**
     * 查询短信日志列表
     * 
     * @param oarSmsQuery 短信日志
     * @return 短信日志
     */
    @Override
    public List<OarSmsDTO> selectOarSmsList(OarSmsQuery oarSmsQuery) {
        return oarSmsMapper.selectOarSmsList(oarSmsQuery).stream().map(this::toDto).collect(Collectors.toList());
    }

    /**
     * 分页查询短信日志列表
     * @param oarSmsQuery 短信日志
     * @return
     */
    @Override
    public TableDataInfo<OarSmsDTO> findAll(OarSmsQuery oarSmsQuery) {
        List<OarSms> list = oarSmsMapper.selectOarSmsList(oarSmsQuery);
        List<OarSmsDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList,new PageInfo(list).getTotal());
    }
    /**
     * 新增短信日志
     * 
     * @param oarSmsDTO 短信日志
     * @return 结果
     */
    @Override
    public int insertOarSms(OarSmsDTO oarSmsDTO) {
        checkOarSms(oarSmsDTO);
        return oarSmsMapper.insertOarSms(toEntity(oarSmsDTO));
    }

    /**
     * 修改短信日志
     * 
     * @param oarSmsDTO 短信日志
     * @return 结果
     */
    @Override
    public int updateOarSms(OarSmsDTO oarSmsDTO) {
        checkOarSms(oarSmsDTO);
        return oarSmsMapper.updateOarSms(toEntity(oarSmsDTO));
    }

    /**
     * 批量删除短信日志
     * 
     * @param ids 需要删除的短信日志主键
     * @return 结果
     */
    @Override
    public int deleteOarSmsByIds(String[] ids) {
        return oarSmsMapper.deleteOarSmsByIds(ids);
    }

    /**
     * 删除短信日志信息
     * 
     * @param id 短信日志主键
     * @return 结果
     */
    @Override
    public int deleteOarSmsById(String id)
    {
        return oarSmsMapper.deleteOarSmsById(id);
    }

    /**
    * 校验
    * @param dto
    */
    private void checkOarSms(OarSmsDTO dto) {

    }

    private OarSms toEntity(OarSmsDTO dto) {
        return BeanUtil.toBean(dto, OarSms.class);
    }

    private OarSmsDTO toDto(OarSms entity) {
        return toDto(entity, false);
    }
    /**
     * 转DTO
     *
     * @param entity
     * @param withDetail 是否查询其他信息
     * @return
     */
    private OarSmsDTO toDto(OarSms entity, Boolean withDetail) {
            OarSmsDTO dto =  BeanUtil.toBean(entity, OarSmsDTO.class);
        if (withDetail) {

        }
        return dto;
    }
}
